﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ghy.Soft.Web.Models;
using Ghy.Soft.IBusiness;
using Ghy.Soft.Business;
using Ghy.Soft.Domain.Entities;
using System.Web.Security;
using Ghy.Soft.Web.Controllers.AopController;

namespace Ghy.Soft.Web.Controllers
{
    public class AdminController : Controller
    {
        [OwnAttribute(AdminPermission.Writer)]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(AdminLoginModel model)
        {
            IAdminBusiness adminBusiness = new AdminBusiness();
            Admin admin = adminBusiness.GetAdminByUsernameAndPassword(model.AdminName, model.Password);
            if (admin != null)
            {
                Session.Add("ticket", String.Format("{0},{1}", admin.AdminId, admin.AdminName));
                Session.Add("permission", admin.Permission);
                return RedirectToAction("Index");
            }
            else
            {
                ModelState.AddModelError("", "登录失败，请检查你的用户名和密码");
                return View(model);
            }
        }

        [OwnAttribute(AdminPermission.Manager), HttpGet]
        public ActionResult AdminView()
        {
            IAdminBusiness adminBusiness = new AdminBusiness();
            ViewData.Add("admins", adminBusiness.GetAllAdmins());
            String[] key = Enum.GetNames(typeof(AdminPermission));
            int i = 0;
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (int v in Enum.GetValues(typeof(AdminPermission)))
            {
                items.Add(new SelectListItem { Text = key[i], Value = v.ToString()});
                i++;
            }
            ViewData.Add("lists", items);
            return View();
        }

        [OwnAttribute(AdminPermission.Manager),HttpGet]
        public ActionResult AdminAdd()
        {
            return View();
        }

        [OwnAttribute(AdminPermission.Manager), HttpPost]
        public ActionResult AdminAdd(Admin admin)
        {
            return View();
        }
    }
}
